<template>
  <a-modal
    title="新增員工"
    :width="760"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading" style="padding:15px 25px;">
      <a-form :form="form" v-bind="formLayout">
        <a-row class="form-row" :gutter="48">
          <div v-for="(v, k) in fields" :key="k">
            <a-col :lg="12" :md="12" :sm="24">
              <a-form-item :label="v.label">
                <template v-if="v.type == 'select'">
                  <a-select
                    v-decorator="[v.key, {initialValue:model?model[v.key]:'',rules: [{required: v.required,message: v.message}]}]"
                    @change="v.onChange">
                    <a-select-option :key="sk" v-for="(item,sk) in selectList[v.listName]" :value="v.listValue?item[v.listValue]:item">{{
                      item[v.listLabel]}}
                    </a-select-option>
                  </a-select>
                </template>
                <template v-else-if="v.type == 'textarea'">
                  <a-textarea :auto-size="{ minRows: 5, maxRows: 10 }" v-decorator="[v.key, {initialValue:model?model[v.key]:'',rules: [{required: v.required,message: v.message}]}]"/>
                </template>
                <template v-else-if="v.type == 'password'">
                  <a-input-password v-decorator="[v.key, {initialValue:model?model[v.key]:'',rules: [{required: v.required,message: v.message}]}]" placeholder="請輸入密碼"/>
                </template>
                <template v-else-if="v.type == 'upload'">
                  <a-upload
                    name="file"
                    :showUploadList="false"
                    :customRequest="upload"
                  >
                    <a-button> <a-icon type="upload" /> Click to Upload </a-button>
                  </a-upload>
                  <span v-if="model!==null">{{ model[v.key] }}</span>
                </template>
                <template v-else-if="v.type == 'date'">
                  <a-date-picker valueFormat="yyyy-MM-DD" v-decorator="[v.key, {initialValue:model?model[v.key]:'',rules: [{required: v.required,message: v.message}]}]" />
                </template>
                <template v-else-if="v.type == 'number'">
                  <a-input v-decorator="[v.key, {initialValue:model?model[v.key]:'',rules: [{required: v.required, min: 0, message: v.message, type: 'number',transform(value){return Number(value)}}]}]"/>
                </template>
                <template v-else>
                  <a-input v-decorator="[v.key, {initialValue:model?model[v.key]:'',rules: [{required: v.required, min: 0, message: v.message}]}]"/>
                </template>
              </a-form-item>
            </a-col>
          </div>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { _getRoleist } from '@/api/role'
import { _post, _put } from '@/api/staff'
// 表单字段
const fields = [
    { key: 'idRol', label: '員工角色', required: true, message: '請選擇職位', type: 'select', listName: 'roleList', listValue: 'idRol', listLabel: 'name', showInEdit: false },
    { key: 'account', label: '賬號', required: true, message: '請輸入員工賬號' },
    { key: 'password', label: '密碼', required: true, message: '請輸入密碼', type: 'password' },
    { key: 'type', label: '員工類型', required: true, message: '請輸入員工姓名', type: 'select', listName: 'typeList', listValue: 'value', listLabel: 'label' },
    { key: 'name', label: '員工姓名', required: true, message: '請輸入員工姓名' },
    { key: 'gender', label: '性別', required: true, message: '', type: 'select', listName: 'genderList', listValue: 'value', listLabel: 'label' },
    { key: 'phone', label: '電話', required: true, message: '' },
    { key: 'workPlace', label: '工作地點', required: true, message: '' },
    { key: 'language', label: '語言', required: true, message: '' },
    { key: 'address', label: '地址', required: true, message: '' },
    { key: 'emergencyContactNumber', label: '緊急聯絡人電話', required: true, message: '' },
    { key: 'header', label: '直屬上級', required: true, message: '' },
    { key: 'headerPhone', label: '上級電話', required: true, message: '' },
    { key: 'salary', label: '工資', required: true, message: '', type: 'number' },
    { key: 'salaryType', label: '工資方式', required: true, message: '', type: 'select', listName: 'salaryTypeList', listValue: 'value', listLabel: 'label' }
]
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
    }
    return {
      form: this.$form.createForm(this),
      fields: fields,
      loading: false,
      selectList: {
        roleList: [],
        genderList: [{ value: 0, label: '女' }, { value: 1, label: '男' }],
        salaryTypeList: [{ value: 0, label: '日新' }, { value: 1, label: '時薪' }],
        typeList: [{ value: 0, label: '後臺人員' }, { value: 1, label: '倉管員' }, { value: 2, label: '車隊管理員' }, { value: 3, label: '司機' }]
      }
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach((v, i) => this.form.getFieldDecorator(v.key))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  mounted () {
    _getRoleist().then((ret) => {
      this.selectList.roleList = ret.data
    })
  },
  methods: {
    // handler
    handleSubmit (e) {
      e.preventDefault()
      this.$emit('handleRefresh')
      this.form.validateFields((err, values) => {
        if (!err) {
          // 新增
          this.loading = true
          if (values.idWar != null) {
             _put(values).then((res) => this.handleSuccess(res))
          } else {
             _post(values).then((res) => this.handleSuccess(res))
          }
        }
      })
    },
    handleSuccess (ret) {
      this.loading = false
      if (ret.code === 0) {
          this.form.resetFields()
          this.$emit('cancel')
          this.$message.info(ret.msg)
      } else {
        this.$message.error(ret.msg)
      }
    }
  }
}
</script>
